import axios from "axios";
import { showNotify } from 'vant';

let instance = axios.create({
    baseURL: "/api",
    timeout: 10000,
})

// 请求拦截器
instance.interceptors.request.use((config) => {
    let token = localStorage.getItem("token")
    if(token){
        config.headers.token = token
    }
    return config
})

// 响应拦截器
instance.interceptors.response.use((res) =>{
    if(res.data?.data?.token){
        localStorage.setItem("token" , res.data.data.token)
    }
    showNotify({ type: res.data.code==="200"?'success':'error', message: res.data.message });
    return res.data
})

export default instance